/*
 * @Author: c
 * @Date: 2024-03-29 04:05:29
 * @LastEditTime: 2024-03-30 03:13:33
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\music\search\search.service.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Injectable } from '@nestjs/common';
import { SearchTypeEnum } from 'src/common/enums';
import { User } from 'src/core/system/user/entities/user.entity';
import { AlbumService } from '../album/album.service';
import { AlbumPageQuery } from '../album/dto/search-album.dto';
import { ArtistService } from '../artist/artist.service';
import { ArtistPageQuery } from '../artist/dto';
import { SongPageQuery } from '../song/dto/search-song.dto';
import { SongService } from '../song/song.service';
import { CreateSearchDto } from './dto/create-search.dto';
import { SearchPageDto, SearchVo } from './dto/search.dto';
import { UpdateSearchDto } from './dto/update-search.dto';

@Injectable()
export class SearchService {
  constructor(
    private songService: SongService,
    private albumService: AlbumService,
    private artistService: ArtistService,
  ) { }

  async search(user: User, { word, type, pageNum = 1, pageSize = 20 }: SearchPageDto): Promise<SearchVo> {

    const searchDto = new SearchVo()
    if (type === SearchTypeEnum.Song) {
      const query = new SongPageQuery()
      query.keywords = word;
      const data = await this.songService.findAll(user, { ...query, pageNum, pageSize });
      searchDto.typeTrack = [...data.list]
      searchDto.total = data.total
      return searchDto;
    }
    if (type === SearchTypeEnum.Album) {
      const query = new AlbumPageQuery()
      query.keywords = word;
      const data = await this.albumService.findAll(user, { ...query, pageNum, pageSize });
      searchDto.typeAlbum = [...data.list]
      searchDto.total = data.total
      return searchDto;
    }
    if (type === SearchTypeEnum.Artist) {
      const query = new ArtistPageQuery()
      query.keywords = word;
      const data = await this.artistService.findAll({ ...query, pageNum, pageSize });
      searchDto.typeArtist = [...data.list]
      searchDto.total = data.total
      return searchDto;
    }

  }
  create(createSearchDto: CreateSearchDto) {
    return 'This action adds a new search';
  }

  findAll() {
    return `This action returns all search`;
  }

  findOne(id: number) {
    return `This action returns a #${id} search`;
  }

  update(id: number, updateSearchDto: UpdateSearchDto) {
    return `This action updates a #${id} search`;
  }

  remove(id: number) {
    return `This action removes a #${id} search`;
  }
}
